package com.yxq.mdt.app.modules.socialcontact.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wuhenzhizao.titlebar.widget.CommonTitleBar;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseActivity;
import com.yxq.mdt.app.glide.GlideHelper;
import com.yxq.mdt.app.modules.socialcontact.adapter.CircleAllListAdapter;
import com.yxq.mdt.app.modules.socialcontact.bean.CircleTopicListBean;
import com.yxq.mdt.app.modules.socialcontact.bean.MainSocialHeadBean;
import com.yxq.mdt.app.modules.socialcontact.bean.SocialAllListMember;
import com.yxq.mdt.app.modules.socialcontact.model.MainSocialContactModel;
import com.yxq.mdt.app.modules.socialcontact.presenter.MainSocialHomeRecommendPresenter;
import com.yxq.mdt.app.modules.socialcontact.presenter.mainSocialHomeRecommendContract;
import com.yxq.mdt.app.net.ApiModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 当前圈子的成员列表
 */
public class CircleMemberListAc extends BaseActivity<MainSocialHomeRecommendPresenter> implements mainSocialHomeRecommendContract.View {
    public static final String EXTRA_TEXT = "extra_text";
    @BindView(R.id.social_like_recycler)
    RecyclerView socialLikeRecycler;
    @BindView(R.id.titlebar)
    CommonTitleBar titlebar;
    @BindView(R.id.membership)
    TextView membership;
    @BindView(R.id.circle_image_head)
    ImageView circleImageHead;
    @BindView(R.id.circle_nike)
    TextView circleNike;
    @BindView(R.id.circle_text_add)
    TextView circleTextAdd;
    @BindView(R.id.announcement)
    TextView announcement;
    @BindView(R.id.refreshLayout_all)
    SmartRefreshLayout refreshLayoutAll;
    private List<SocialAllListMember.DataBean.ListBean> socialAllListMembers;
    private String memberNum;
    CircleAllListAdapter circleAllListAdapter;
    private List<MainSocialHeadBean.MenberListBean> mainSocialHeadBeans;

    @Override
    public int getLayoutId() {
        return R.layout.activity_circle_all_list;
    }

    @Override
    public void initView() {
        memberNum = getIntent().getStringExtra("memberNum");
        socialAllListMembers = new ArrayList<>();
        mainSocialHeadBeans = new ArrayList<>();
        circleAllListAdapter = new CircleAllListAdapter(R.layout.item_circle_all_list, socialAllListMembers);
        socialLikeRecycler.setLayoutManager(new LinearLayoutManager(mActivity, LinearLayoutManager.VERTICAL, false));
        socialLikeRecycler.setAdapter(circleAllListAdapter);
        refreshLayoutAll.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getQueryGroupMembers(20, memberNum, 1);
        });
        refreshLayoutAll.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.getQueryGroupMembers(20, memberNum, page);
        });

    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mPresenter = new MainSocialHomeRecommendPresenter(this, new MainSocialContactModel());
        mPresenter.getRecommendGroupDetail(memberNum);
        refreshLayoutAll.autoRefresh();
    }

    @Override
    public void onLoadRecommendGroupDetailSuccess(MainSocialHeadBean mainSocialHeadBean) {
        if (mainSocialHeadBean != null) {
            GlideHelper.setLoadRoundImage(mActivity, mainSocialHeadBean.getLogo(), R.mipmap.head_icon, circleImageHead);
            circleNike.setText(mainSocialHeadBean.getGroupname());
            announcement.setText(mainSocialHeadBean.getDescription());
        }
    }

    @Override
    public void onLoadRecommendGroupDetailFailed() {

    }

    @Override
    public void onLoadTopListSuccess(CircleTopicListBean circleTopicListBean) {

    }

    @Override
    public void onLoadTopListFailed() {

    }

    @Override
    public void onLoadApplyGroupSuccess(String s) {
        mPresenter.getQueryGroupMembers(20, memberNum, page);
    }

    @Override
    public void onLoadApplyGroupFailed() {

    }

    @Override
    public void onLoadIntroductionGroupMembersSuccess(String s) {
        mPresenter.getQueryGroupMembers(20, memberNum, page);
    }

    @Override
    public void onLoadIntroductionGroupMembersFailed() {

    }

    @Override
    public void onLoadQueryGroupMembersSuccess(SocialAllListMember socialAllListMember) {
        refreshLayoutAll.finishLoadMore();
        refreshLayoutAll.finishRefresh();
        if (page == 1) {
            circleAllListAdapter.setNewData(socialAllListMember.getData().getList());
        } else {
            if (socialAllListMember.getData() != null && socialAllListMember.getData().getList().size() != 0) {
                circleAllListAdapter.addData(socialAllListMember.getData().getList());
            } else {
                refreshLayoutAll.finishLoadMoreWithNoMoreData();
            }
        }

        if (socialAllListMember.isIsJoin()) {
            circleTextAdd.setText("退出");
        } else {
            circleTextAdd.setText("加入");

        }
        circleTextAdd.setOnClickListener(view -> {
            if (socialAllListMember.isIsJoin()) {
                mPresenter.setIntroductionGroupMembers(memberNum);
            } else {
                mPresenter.setApplyGroup(memberNum);
            }
        });

        circleAllListAdapter.setOnItemChildClickListener((BaseQuickAdapter adapter, View view1, int position) -> {
            switch (view1.getId()) {
                case R.id.circle_img_all:
                    Intent it = new Intent(mActivity, SocialPersonalHomepageAc.class);
                    it.putExtra("userId", circleAllListAdapter.getItem(position).getUserId());
                    startActivity(it);
                    break;
                case R.id.circle_attention_tv_all:
                    if (socialAllListMember.getData().getList().get(position).getCompany() == 0) {
                        mPresenter.setInterestRelation(circleAllListAdapter.getItem(position).getUserId(), 1, 1);
                    } else {
                        mPresenter.setInterestRelation(circleAllListAdapter.getItem(position).getUserId(), 0, 1);
                    }
                    break;
                default:
            }
        });
        membership.setText("(" + socialAllListMember.getData().getTotal() + ")");

        circleAllListAdapter.isUseEmpty(!(circleAllListAdapter.getData().size() > 0));
        circleAllListAdapter.notifyDataSetChanged();
    }

    @Override
    public void onLoadQueryGroupMembersFailed() {

    }

    @Override
    public void onLoadRelationSuccess(String s) {
        showBottomToast("操作成功");
        mPresenter.getQueryGroupMembers(20, memberNum, page);
    }

    @Override
    public void onLoadRelationFailed() {

    }

}
